package com.ruoyi.wbs.service.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.wbs.mapper.WbsPlanMapper;
import com.ruoyi.wbs.domain.WbsPlan;
import com.ruoyi.wbs.service.IWbsPlanService;

/**
 * WBS计划Service业务层处理
 * 
 * @author zhaogang
 * @date 2024-07-18
 */
@Service
public class WbsPlanServiceImpl implements IWbsPlanService 
{
    @Autowired
    private WbsPlanMapper wbsPlanMapper;

    /**
     * 查询WBS计划
     * 
     * @param wbsId WBS计划主键
     * @return WBS计划
     */
    @Override
    public WbsPlan selectWbsPlanByWbsId(Long wbsId)
    {
        return wbsPlanMapper.selectWbsPlanByWbsId(wbsId);
    }

    /**
     * 查询WBS计划列表
     * 
     * @param wbsPlan WBS计划
     * @return WBS计划
     */
    @Override
    public List<WbsPlan> selectWbsPlanList(WbsPlan wbsPlan)
    {
        return wbsPlanMapper.selectWbsPlanList(wbsPlan);
    }

    /**
     * 新增WBS计划
     * 
     * @param wbsPlan WBS计划
     * @return 结果
     */
    @Override
    public int insertWbsPlan(WbsPlan wbsPlan)
    {
        return wbsPlanMapper.insertWbsPlan(wbsPlan);
    }

    /**
     * 修改WBS计划
     * 
     * @param wbsPlan WBS计划
     * @return 结果
     */
    @Override
    public int updateWbsPlan(WbsPlan wbsPlan)
    {
        return wbsPlanMapper.updateWbsPlan(wbsPlan);
    }

    /**
     * 批量删除WBS计划
     * 
     * @param wbsIds 需要删除的WBS计划主键
     * @return 结果
     */
    @Override
    public int deleteWbsPlanByWbsIds(Long[] wbsIds)
    {
        ArrayList<Object> paths = new ArrayList<>();
        for(Long wbsId:wbsIds){
            String pic = wbsPlanMapper.selectWbsPlanByWbsId(wbsId).getWbsPic();
            if (StringUtils.isNotBlank(pic)) {
                String path = pic.substring( pic.indexOf("/",1));
                paths.add(path);
            }
        }

      int i =  wbsPlanMapper.deleteWbsPlanByWbsIds(wbsIds);

        if (i > 0){
            paths.forEach(path -> {
                File file = new File(RuoYiConfig.getProfile() + path);
                if (file.exists()){
                    file.delete();
                }
            });
        }
        return i;
    }

    /**
     * 删除WBS计划信息
     * 
     * @param wbsId WBS计划主键
     * @return 结果
     */
    @Override
    public int deleteWbsPlanByWbsId(Long wbsId)
    {
        return wbsPlanMapper.deleteWbsPlanByWbsId(wbsId);
    }
}
